package cn.czh0123.service.impl;

import cn.czh0123.domain.MessageBoard;
import cn.czh0123.domain.User;
import cn.czh0123.mapper.MessageBoardMapper;
import cn.czh0123.query.MessageBoardQuery;
import cn.czh0123.service.IMessageBoardService;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
* 留言板服务接口实现
*/
@Service
@Transactional
public class MessageBoardServiceImpl extends ServiceImpl<MessageBoardMapper, MessageBoard> implements IMessageBoardService {

    @Resource
    private MessageBoardMapper messageMapper;

    @Override
    public IPage<MessageBoard> queryPage(MessageBoardQuery qo) {
        IPage<MessageBoard> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return messageMapper.selectMessageBoardPage(page, qo);
    }

    @Override
    public MessageBoard saveMessageBoard(MessageBoard messageBoard) {
        if (StpUtil.isLogin()) {
            User user = (User) StpUtil.getSession().get("user");
            messageBoard.setUserId(user.getId());
            messageBoard.setUserName(user.getUsername());
        } else {
            messageBoard.setUserId(0L);
            messageBoard.setUserName("游客");
        }
        messageBoard.setPostTime(new Date());
        messageMapper.insert(messageBoard);
        return messageBoard;
    }
}
